#include "stdafx.h"
#include "HookFunctions.h"
#include "HookDll.h"
#include "..\Common\Common.h"
#include "DataDef.h"


extern bool g_bHooked;
extern HOOK_TYPE_e g_HookType;

 LocalReAllocDef pOrgLocalReAlloc = 0;
 GlobalAllocDef pOrgGlobalAlloc = 0;
 LocalAllocDef pOrgLocalAlloc = 0;
 GlobalReAllocDef pOrgGlobalReAlloc = 0;

HANDLE WINAPI MyGlobalAlloc( UINT uFlags, SIZE_T dwBytes )
{
    HANDLE hHandle = pOrgGlobalAlloc( uFlags,  dwBytes );
    if ( g_HookType == HT_MEMORY )
        CreateCallStack(hHandle, dwBytes);
    else
        CreateCallStack(hHandle, TYPE_MEMORY_HANDLE );
    return hHandle;
}

HANDLE WINAPI MyGlobalReAlloc( HGLOBAL hMem, SIZE_T dwBytes, UINT uFlags )
{
    HANDLE hHandle = pOrgGlobalReAlloc( hMem, dwBytes, uFlags  );
    if( hHandle )
    {
        if ( g_HookType == HT_MEMORY )
            CreateCallStack(hHandle, dwBytes);
        else
            CreateCallStack(hHandle, TYPE_MEMORY_HANDLE );
        RemoveCallStack( hMem );
    }
    return hHandle;
}


HLOCAL WINAPI MyLocalReAlloc( HLOCAL hMem, SIZE_T uBytes, UINT uFlags )
{
    HLOCAL hHandle = pOrgLocalReAlloc( hMem, uBytes, uFlags);
    if( hHandle )
    {
        if ( g_HookType == HT_MEMORY )
            CreateCallStack(hHandle, uBytes);
        else
            CreateCallStack(hHandle, TYPE_MEMORY_HANDLE );
        RemoveCallStack( hMem );
    }
    return hHandle;
}

HLOCAL WINAPI MyLocalAlloc( UINT uFlags, SIZE_T uBytes )
{
    HLOCAL hHandle = pOrgLocalAlloc( uFlags,  uBytes );
    if ( g_HookType == HT_MEMORY )
        CreateCallStack(hHandle, uBytes);
    else
        CreateCallStack(hHandle, TYPE_MEMORY_HANDLE );
    return hHandle;
}